<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="auto" size="small">

      <el-form-item :label="$t('u1')" prop="nickName">
        <el-input
          v-model="queryParams.userName"
          clearable
          :placeholder="$t('input')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{$t("search")}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t("reset")}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:receptionUser:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >{{$t("add")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:receptionUser:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >{{$t("edit")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:receptionUser:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >{{$t("delete")}}
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receptionUserList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" :label="$t('u155')" prop="userId"/>
      <el-table-column align="center" :label="$t('u1')" prop="userName"/>

      <el-table-column align="center" :label="$t('u2')" prop="remark"/>

      <el-table-column align="center" class-name="small-padding fixed-width" :label="$t('controls')">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:receptionUser:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >{{$t("edit")}}
          </el-button>
          <el-button
            v-hasPermi="['system:receptionUser:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handlePassword(scope.row)"
          >{{$t("resetPassword")}}
          </el-button>
          <el-button
            v-hasPermi="['system:receptionUser:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >{{$t("delete")}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('u2')" prop="parentId">
          <treeselect v-model="form.deptId" :normalizer="normalizer" :options="deptOptions" :placeholder="$t('input')"/>
        </el-form-item>
        <el-row>

            <el-form-item :label="$t('dd9')" prop="phonenumber">
              <el-input v-model="form.phonenumber" maxlength="11"   :placeholder="$t('input')"/>
            </el-form-item>




        </el-row>
        <el-form-item :label="$t('u4')" prop="userName">
          <el-input v-model="form.userName" :placeholder="$t('input')"/>
        </el-form-item>

        <el-form-item :label="$t('u3')">
          <el-select v-model="form.roleIds" multiple   :placeholder="$t('select')">
            <el-option
              v-for="item in roleOptions"
              :key="item.roleId"
              :disabled="item.status == 1"
              :label="item.roleName"
              :value="item.roleId"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t("ok")}}</el-button>
        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open2" append-to-body width="500px">
      <el-form ref="form" :model="form"  label-width="auto">



        <el-form-item :label="$t('u5')" prop="password">
          <el-input v-model="form.password" :placeholder="$t('input')" type="password"/>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t("ok")}}</el-button>
        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReceptionDept} from "@/api/system/receptionDept";
import {
  addReceptionUser,
  delReceptionUser,
  getReceptionUser,
  listReceptionUser,
  updateReceptionUser
} from "@/api/system/receptionUser";
import {listReceptionRole} from "@/api/system/receptionRole";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ReceptionUser",
  components: {
    Treeselect
  },
  data() {
    return {
      open2:false,
      roleOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      receptionUserList: [],
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        nickName: null,
        parentId: null,
      },
      // 表单参数
      form: {

        roleIds:[]
      },
      // 表单校验
      rules: {
        deptId: [
          {required: true, message: this.$t('menu12'), trigger: "blur"}
        ],
        userName: [
          {required: true, message: this.$t('menu13'), trigger: "blur"}
        ],
        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: this.$t('us8'),
            trigger: "blur"
          }
        ],
        roleIds: [
          {

            required: true,
            message: this.$t('menu18'),
            trigger: "blur,change",
          }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listReceptionUser(this.queryParams).then(response => {
        this.receptionUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {

        parentId: null,
        userId: null,
        deptId: null,
        userName: null,
        nickName: null,
        password: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
          roleIds: []

      };
      this.resetForm("form");
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();

      listReceptionRole(null).then(response => {
        this.roleOptions = response.rows;

        this.open = true;
        this.title = this.$t('u6');
      });


    },
    handlePassword(row){
      this.reset();
      this.getTreeselect();

      const userId = row.userId || this.ids
      getReceptionUser(userId).then(response => {

          this.form = response.data;


          this.roleOptions = response.roleIds;

          this.$set(this.form, "roleIds", response.roleId);

          this.open2 = true;
          this.title = "";
    })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();

      const userId = row.userId || this.ids
      getReceptionUser(userId).then(response => {

        this.form = response.data;


        this.roleOptions = response.roleIds;

        this.$set(this.form, "roleIds", response.roleId);

        this.open = true;
        this.title = this.$t('u7');
      });
    },
    getTreeselect() {
      listReceptionDept().then(response => {
        this.deptOptions = [];
        const data = {deptId: response.data[0].deptId, deptName: response.data[0].deptName, children: []};
        data.children = this.handleTree(response.data, "deptId", "parentId");
        this.deptOptions.push(data);

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateReceptionUser(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
              this.open = false;
              this.open2=false
              this.getList();
            });
          } else {
            addReceptionUser(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('newSuccessfullyAdded'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm(this.$t('u8') +userIds ).then(function () {
        return delReceptionUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('de1'));
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/receptionUser/export', {
        ...this.queryParams
      }, `receptionUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
